package com.nstar.sinaweibo.model
{
	import com.nstar.base.util.DateUtils;
	import com.nstar.base.util.LoadXMLDelegate;
	import com.nstar.base.util.Logger;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.IResponder;
	
	
	/**
	 * 节假日、一些特殊日期的处理
	 * 在一些比较特殊的日子里，显示指定的logo，或者问候语
	 * 
	 */ 
	public class HolidayLoader extends EventDispatcher  implements IResponder
	{
		private var loader:LoadXMLDelegate;
		private var url:String ;
		private var logger:Logger = Logger.getInstance(this);
		
		private var _defaultLogo:HolidayVO;
		
		private var _holidays:ArrayCollection = new ArrayCollection();
				
		public function HolidayLoader(url:String)
		{
			trace("holidays config url : "+url);
			this.url = url;
			loader = new LoadXMLDelegate(this,this.url,"object");
			loader.load();
		}
		
		public function result(data:Object):void{
			_holidays.removeAll();
			_defaultLogo = getDefaultLogo(data.result.holidays.defaultLogo);
			var tempHolidays:ArrayCollection = data.result.holidays.holiday;
			logger.debug("load holidays config xml ok. ["+tempHolidays.length+" holidays]");
			for each(var item:Object in tempHolidays){
				var holidayVO:HolidayVO = new HolidayVO();
				holidayVO.seq = item.seq;
				holidayVO.name = item.name;
				holidayVO.date = item.date;
				holidayVO.logo = item.logo;
				holidayVO.url = item.url;
				holidayVO.description = item.description;
				_holidays.addItem(holidayVO);
			}
			
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		
		public function getDefaultLogo(item:Object):HolidayVO{
			if (!item) return null;
			
			var holidayVO:HolidayVO = new HolidayVO();
			holidayVO.seq = item.seq;
			holidayVO.name = item.name;
			holidayVO.date = item.date;
			holidayVO.logo = item.logo;
			holidayVO.url = item.url;
			holidayVO.description = item.description;
			return holidayVO;
		}
		
		public function get defaultLogo():HolidayVO{
			return _defaultLogo;
		}
		
		public function fault(info:Object):void{
			logger.debug("load holidays config xml error!!"+info.fault.faultDetail);
		}
		
		public function get holidays():ArrayCollection{
			return this._holidays;
		}
		
		//是否有今天的
		public function getDay(date:Date):HolidayVO{
			if(date){
				var dateStr:String = DateUtils.format(date,"MM-DD");
				for each(var holiday:HolidayVO in _holidays){
					if(dateStr == holiday.date){
						return holiday;
					}
				}
			}
			return null;
		}
		
	}
}